#pragma once

class AddEmailHandler : public JsonRequest<AddEmailHandler> 
{
public:
	bool response()
	{
		std::string		email;
		std::string		smtp;
		std::string		smtp_port;
		std::string		pop;
		std::string		pop_port;
		std::string		user;
		std::string		pwd;
		sql_string<>	sql;

		if( false == request::post("email", email)
				|| false == request::post("smtp", smtp)
				|| false == request::post("smtp_port", smtp_port)
				|| false == request::post("pop", pop)
				|| false == request::post("pop_port", pop_port)
				|| false == request::post("user", user)
				|| false == request::post("pwd", pwd) )
		{
			err << "invalid paramter";
			return false;
		}
		
		sql.format("insert into t_emails(addr, smtp_ip, smtp_port, pop_ip,"
				" pop_port, user, pwd)"
				" values('%q', '%q', %d, '%q', %d, '%q', '%q')",
				email.c_str(),
				smtp.c_str(),
				atoi(smtp_port.c_str()),
				pop.c_str(),
				atoi(pop_port.c_str()),
				user.c_str(),
				pwd.c_str());
		if(1 != sql::execute(sql.c_str()))
		{
			err << "add email failed:"
				<< sql.c_str();
			return false;
		}
		
		json.push_back(JSONNode("errno", 0));
		return true;
	}
};
